package com.spark.common.helper

import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonElement
import com.google.gson.JsonObject

abstract class JsonBox {
    abstract fun fromJson(context: JsonDeserializationContext, jo: JsonObject)
    protected fun <T> filling(context: JsonDeserializationContext, toMany: ArrayList<T>, type: Class<T>, jsonEl: JsonElement) {
        toMany.clear()
        if (jsonEl.isJsonArray) {
            val ja = jsonEl.asJsonArray
            ja.forEach {
                var item: T = context.deserialize(it, type)
                toMany.add(item)
            }
        }
    }

    /*protected fun <T : JsonBox> filling(toOne: ToOne<T>?, typeOfT: Type, jsonElement: JsonElement, context: JsonDeserializationContext) {
        if (toOne == null)
            return
        try {
            val one = (typeOfT as Class<T>).newInstance()
            one.fromJson(jsonElement.asJsonObject, context)
            toOne.setTarget(one)
        } catch (e: InstantiationException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
    }*/
}